package yyl.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import yyl.domain.User;
import yyl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@Controller
public class UserController {
    @Autowired
    @Qualifier("userService")
    private UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ModelAndView login(String loginname, String password, ModelAndView mv, Model model){
        User user = userService.login(loginname, password);
        if (user != null){
            model.addAttribute("user", user);
            mv.setView(new RedirectView("/cat/main"));
        } else {
            mv.addObject("message", "登陆名或密码错误，请重新输入！");
            mv.setViewName("loginForm");
        }
        return mv;
    }

}
